package com.qf.service.impl;

import com.qf.dao.repository.XcCityRepository;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.XcCityService;
import com.qf.service.XcJqCityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 城市
 * @author WYD
 * @date 2021/10/25  20:47
 */
@Service
public class XcJqCityServiceImpl implements XcJqCityService {

    @Autowired
    XcCityRepository xcCityRepository;

    @Autowired
    RedisTemplate redisTemplate;

    protected Logger logger = LoggerFactory.getLogger(XcJqServiceImpl.class);

    /**
     * 城市遍历
     * @return
     */
    @Override
    public BaseResp findAll() {
        List list = new ArrayList();
        Boolean xc_city_all = redisTemplate.hasKey("XC_CITY_ALL");
        if (!xc_city_all){
            //数据库查询
            list = xcCityRepository.findAll();
            logger.info("从数据库查询的结果集为:"+list);
            //添加入库
            redisTemplate.opsForList().leftPushAll("XC_CITY_ALL",list);
            //设置存活时间
            redisTemplate.expire("XC_CITY_ALL",10, TimeUnit.SECONDS);
        }else {
            //遍历
            list=redisTemplate.opsForList().range("XC_CITY_ALL",0,-1);
            logger.info("从redis中查询的结果集为:"+list);
        }
        System.out.println(list);
        return new BaseResp(0,"查询成功",null,list);
    }

}
